package com.ldc.wandroid.ui.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;

import com.ldc.common.model.BaseModel;
import com.ldc.common.mvvm.BaseMvvmFragment;
import com.ldc.common.utils.ViewModelUtils;
import com.ldc.wandroid.adapter.WeChatNumberAdapter;
import com.ldc.wandroid.databinding.FragmentWeChatNumberBinding;
import com.ldc.wandroid.model.WeChatNumberModel;
import com.ldc.wandroid.viewmodel.WeChatNumberViewModel;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class WeChatNumberFragment extends BaseMvvmFragment<FragmentWeChatNumberBinding, WeChatNumberViewModel> {
    public static WeChatNumberFragment newInstance() {
        Bundle args = new Bundle();
        WeChatNumberFragment fragment = new WeChatNumberFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private WeChatNumberAdapter weChatNumberAdapter;

    @Override
    protected WeChatNumberViewModel initViewModel() {
        return ViewModelUtils.getViewModel(this, WeChatNumberViewModel.class);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        viewModel.getResultData().removeObserver(this::getWechatNumberResp);

    }

    @Override
    protected void main(Bundle savedInstanceState) {
        viewBinding.stateView.showLoadingView();
        viewModel.getResultData().observeForever(this::getWechatNumberResp);
        weChatNumberAdapter = new WeChatNumberAdapter(getChildFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        viewModel.getWechatNumberReq();
    }

    public void getWechatNumberResp(BaseModel<List<WeChatNumberModel>> dts) {
        viewBinding.stateView.showContentView();
        if (null == dts) return;
        if (dts.isSuccess()) {
            initAdapter(dts.getData());
        }

    }


    //初始化适配器
    private void initAdapter(List<WeChatNumberModel> dts) {
        final ArrayList<String> tabs = new ArrayList<>(16);
        final ArrayList<Fragment> fragments = new ArrayList<>(16);
        viewBinding.tabLayout.removeAllTabs();
        for (WeChatNumberModel model : dts) {
            if (null == model) return;
            final Fragment fragment = new WeChatNumberHistoryFragment();
            final Bundle bundle = new Bundle();
            bundle.putString("cid", String.format("%s", model.getId()));
            bundle.putString("name", String.format("%s", model.getName()));
            fragment.setArguments(bundle);
            //
            viewBinding.tabLayout.addTab(viewBinding.tabLayout.newTab().setText(model.getName()));
            fragments.add(fragment);
            tabs.add(model.getName());
        }
        if (null == weChatNumberAdapter) {
            weChatNumberAdapter = new WeChatNumberAdapter(getChildFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        } else {
            weChatNumberAdapter.setNewData(fragments, tabs);
        }
        viewBinding.fragmentContainer.setOffscreenPageLimit(6);
        viewBinding.fragmentContainer.setCurrentItem(0);
        viewBinding.fragmentContainer.setAdapter(weChatNumberAdapter);
        viewBinding.tabLayout.setupWithViewPager(viewBinding.fragmentContainer);
    }

    @Override
    protected FragmentWeChatNumberBinding initViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return FragmentWeChatNumberBinding.inflate(getLayoutInflater(), container, false);
    }

}
